package android.slc.module.sys.ui.dict.data

import android.slc.appbase.ui.widget.spinner.SpinnerNumberWrapper
import android.slc.appdb.sys.enums.EnableEnum
import android.slc.appdb.utils.DictEnumUtils
import android.slc.box.core.property.DataChangeListener
import javafx.beans.InvalidationListener
import javafx.scene.control.Button
import javafx.scene.control.ComboBox
import javafx.scene.control.Spinner
import javafx.scene.control.TextField
import javafx.scene.layout.Region

class DictDataEditVd(val boxContentPane: Region, val propertyParentPane: Region, val viewModel:DictDataEditVm) {

    lateinit var etStatus: ComboBox<String>

    lateinit var etSort: Spinner<Int>
    private lateinit var spinnerNumberWrapper: SpinnerNumberWrapper

    lateinit var etRemark: TextField

    lateinit var btnOk: Button

    lateinit var btnCancel: Button

    fun initView(){
        this.etStatus= this.propertyParentPane.lookup("#etStatus") as ComboBox<String>
        this.etStatus.items.setAll(viewModel.getStatusList())
        this.etStatus.selectionModel.selectedIndexProperty().addListener(DataChangeListener {
            viewModel.setStatus(it.toInt())
        })
        //字典状态监听
        this.viewModel.dictStatusProperty.addListener(InvalidationListener {
            this.etStatus.selectionModel.select(
                DictEnumUtils.getValueIndex(
                    EnableEnum.values(),
                    this.viewModel.dictStatusProperty.value
                )
            )
        })

        //绑定排序
        this.etSort = this.propertyParentPane.lookup("#etSort") as Spinner<Int>
        this.spinnerNumberWrapper = SpinnerNumberWrapper(this.etSort,0,10000)
        this.spinnerNumberWrapper.valueProperty.bindBidirectional(viewModel.dictSortProperty)
        this.spinnerNumberWrapper.applyControl()

        this.etRemark= this.propertyParentPane.lookup("#etRemark") as TextField
        this.etRemark.textProperty().bindBidirectional(viewModel.dictRemarkProperty)

        this.btnOk = this.boxContentPane.lookup("#btnOk") as Button
        this.btnOk.setOnAction {
            this.viewModel.save()
        }
        this.btnCancel = this.boxContentPane.lookup("#btnCancel") as Button
        this.btnCancel.setOnAction {
            viewModel.finishView()
        }
    }
}